package com.huayue.controller;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.RoomType;
import com.huayue.service.RoomTypeService;
import com.huayue.util.DateHelper;
import com.huayue.util.FileUploadHelper;
import com.huayue.util.PageHelper;

/**
 * 房间类型
 */
@Controller
@RequestMapping("/roomType")
public class RoomTypeController {
    @Autowired
    RoomTypeService svc;

    BaseController bc = new BaseController();

    @RequestMapping("/toAdd")
    public ModelAndView toAdd() {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("status", 200);
        view.addStaticAttribute("room_product_type", svc.findRoom_product_type());
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "添加-房间类型")
    @RequestMapping("/add")
    public ModelAndView add(RoomType room_type) {
        return bc.add(svc, room_type);
    }

    @LogAnno(operateType = "修改-房间类型")
    @RequestMapping("/update")
    public ModelAndView update(RoomType room_type) {
        return bc.update(svc, room_type);
    }

    @LogAnno(operateType = "删除-房间类型")
    @RequestMapping("/delete")
    public ModelAndView delete(RoomType room_type) {
        return bc.delete(svc, room_type);
    }

    @RequestMapping(value = "/findOne", produces = {"text/javascript;charset=UTF-8"})
    @ResponseBody
    public ModelAndView findOne(RoomType room_type) {
        return bc.findOne(svc, room_type);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, RoomType room_type) {
        return bc.findPage(svc, page, room_type, room_type);
    }

    @LogAnno(operateType = "上传-房态图片")
    @RequestMapping("/uploadIcon")
    public ModelAndView uploadIcon(@RequestParam("file") MultipartFile file, HttpServletRequest req) {
        FastJsonJsonView view = new FastJsonJsonView();
        MultipartHttpServletRequest mReq = (MultipartHttpServletRequest)req;
        view.addStaticAttribute("code", 0);
        String fileName = FileUploadHelper.fileUpload(file, req);
        if (fileName != null) {
            RoomType info = new RoomType();
            info.setIcon(fileName);
            String id = mReq.getParameter("pathId");
            info.setId(Integer.valueOf(id));
            if (svc.updateIcon(info) > 0) {
                view.addStaticAttribute("icon", "1");
                view.addStaticAttribute("msg", "文件上传成功！");
                return new ModelAndView(view);
            }
        }
        view.addStaticAttribute("icon", "2");
        view.addStaticAttribute("msg", "文件上传失败！");
        return new ModelAndView(view);
    }

    @RequestMapping("/findTotalPrice")
    public ModelAndView findTotalPrice(PageHelper page, RoomType room_type, String dateRange) {
        FastJsonJsonView view = new FastJsonJsonView();
        String begin = null;
        String end = null;
        if (dateRange != null && !"".equals(dateRange)) {
            begin = dateRange.split(" - ")[0];
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(DateHelper.StringToDateShort(dateRange.split(" - ")[1]));
            calendar.add(Calendar.DATE, 1);
            end = DateHelper.DateToStringShort(calendar.getTime());
        }

        PageHelper pages = new PageHelper();
        pages.setCount(svc.findTotalPriceCount(room_type, begin, end));
        pages.setPageNow(page.getPageNow() == 0 ? 1 : Integer.valueOf(page.getPageNow()));
        pages.setPageSize(page.getPageSize() == 0 ? 5 : Integer.valueOf(page.getPageSize()));
        view.addStaticAttribute("code", 0);
        view.addStaticAttribute("message", "查询成功");
        view.addStaticAttribute("count", pages.getCount());
        view.addStaticAttribute("data", svc.findTotalPrice(pages, room_type, begin, end));
        return new ModelAndView(view);
    }

}
